#include "LogicRecvThread.h"
#include "LogicData.h"
#include "scene/SceneManager.h"
#include <assert.h>

CLogicRecvThread::CLogicRecvThread(void) :
	m_LogicManager(CGLLogicManager::GetInstance()),
	m_LogicConnect(CLogicData::GetInstance()->GetLogicConnect()), m_Handle(nullptr)
{

}


CLogicRecvThread::~CLogicRecvThread(void)
{
	if (nullptr == m_Handle)
		chpktRelease(m_Handle);
}

void CLogicRecvThread::Init()
{
	m_Handle = chpktCreate(SCOKET_MAX_BUFF_LEN);
	if (nullptr == m_Handle) {
		assert(false);
	}
}

void CLogicRecvThread::Run(mail_t mail)
{
	int32_t tLen = m_LogicConnect->Recv(m_Buf, SCOKET_MAX_BUFF_LEN);
	if (tLen <= 0) {

		return;
	}

	chpkgPushData(m_Handle, m_Buf, tLen);
	while (chpkgHasComplatePacket(m_Handle)) {
		uint32_t tCompLen = chpkgGetCurrentPacketLength(m_Handle);
		char* tCompData = chpkgGetCurrentPacketPtr(m_Handle);
		m_LogicManager->HandleLogic(tCompData, tCompLen, m_LogicConnect);
		chpkgCurrentPacketDone(m_Handle);
	}
}
